﻿
#region 作者和版权
/*************************************************************************************
* CLR 版本: 4.0.30319.42000
* 类 名 称: DbHelper
* 机器名称: SUNTIA-QL
* 命名空间: ServerFramework.Db
* 文 件 名: DbHelper
* 创建时间: 2015/9/24 14:51:48
* 作 者: 乔龙 Qiaolong
* 版 权: DbHelper说明：本代码版权归乔龙所有，使用时必须带上乔龙网站地址 All Rights Reserved (C) 2015 - 2013
* 签 名: To be or not, it is not a problem !
* 网 站: http://www.suntia.net
* 邮 箱: qiaolong306@qq.com 
* 唯一标识： 8b2a1dce-76ac-4c7a-9c84-79569ddd3c79 
*
* 登录用户: simida
* 所 属 域: SUNTIA-QL

* 创建年份: 2015
* 修改时间:
* 修 改 人:
* 
************************************************************************************/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using ServerFramework.Tools;


namespace ServerFramework.Db
{
    /// <summary>
    /// 数据库工具类
    /// </summary>
    public static class DbHelper
    {
        /// <summary>
        /// db工厂方法
        /// </summary>
        /// <param name="providerName"></param>
        /// <returns></returns>
        public static DbProvider CreateDbProvider(ConnectionStringSettings dbSetting)
        {
            DbProvider provider = null;
            if (dbSetting.ProviderName == "MySqlDataProvider")
            {
                provider = new DbMySqlProvider(dbSetting.ConnectionString);
            }
            return provider;
        }

        /// <summary>
        /// db工厂方法
        /// </summary>
        /// <param name="providerName"></param>
        /// <returns></returns>
        public static DbProvider CreateDbProvider(ConnectionStringSettings dbSetting, string connectStr)
        {
            DbProvider provider = null;
            if (dbSetting.ProviderName == "MySqlDataProvider")
            {
                provider = new DbMySqlProvider(connectStr);
            }
            return provider;
        }


        public static DbProvider CreateDbProvider(string providerName,string connectString)
        {
            DbProvider provider = null;
            if (providerName == "MySqlDataProvider")
            {
                provider = new DbMySqlProvider(connectString);
            }
            return provider;
        }

        public static DbProvider CreateDbProvider(string connName)
        {
            ConnectionStringSettings connSet = ConfigUtil.GetConnectionSetting(connName);
            return CreateDbProvider(connSet);
        }

        public static void ExecuteQuery(string connName, Action<DbProvider> execute)
        {
            DbProvider db = CreateDbProvider(connName);
            db.Open();
            if (execute != null)
            {
                execute(db);
            }
            db.Close();
        }
    }
}


